$(function () {
    $("#login").click(function () {
        login();
    });
    $(document).keypress(function (e) {
        // 回车键事件
        if (e.which == 13) {
            login();
        }
    });
    layui.form.render();
});

function login() {
    layui.use('layer', function () {
        var username = $("#username").val();
        if (!username) {
            layer.msg('请输入用户名', {
                icon: 5
            });
            return;
        }
        var password = $("#password").val();
        if (!password) {
            layer.msg('请输入密码', {
                icon: 5
            });
            return;
        }
        $.ajax({
            type: "POST",
            url: ctx + "login",
            data: {
                username: username,
                password: password
            },
            dataType: "json",
            success: function (data) {
                if (data.code == 0) {
                    layer.load(1, {
                        shade: [0.1, '#fff'] //0.1透明度的白色背景
                    });
                    parent.location.href = "index";
                } else {
                    layer.msg(data.msg, {
                        icon: 2
                    });
                }
            }
        });
    });
}